import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')

// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
const Users = () => import(/* webpackChunkName:"user" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName:"power" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName:"power" */ '../components/power/Roles.vue')

// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
// import List from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')
const List = () => import(/* webpackChunkName:"goods" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')

// import Order from '../components/order/Order.vue'
// import Report from '../components/report/Report.vue'
const Order = () => import(/* webpackChunkName:"order_report" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName:"order_report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    // 重定向，当用户访问localhost:8080时，会重定向到login界面
    redirect:'/login' 
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {
        path:'/welcome',
        component:Welcome
      },
      {
        path:'/users',
        component:Users
      },
      {
        path:'/roles',
        component:Roles
      },
      {
        path:'/rights',
        component:Rights
      },
      {
        path:'/categories',
        component:Cate
      },
      {
        path:'/params',
        component:Params
      },
      {
        path:'/goods',
        component:List,
      },
      {
        path:'/goods/add',
        component:Add
      },
      {
        path:'/orders',
        component:Order
      },
      {
        path:'/reports',
        component:Report
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  // console.log(to,from)
  if(to.path === '/login') next()
  if(to.path === '/goods/params') next('/params')
  if(to.path === '/goods/categories') next('/categories')
  if(to.path === '/goods/goods') next('/goods')
  if(to.path === '/goods/users') next('/users')
  if(to.path === '/goods/roles') next('/roles')
  if(to.path === '/goods/rights') next('/rights')
  if(to.path === '/goods/orders') next('/orders')
  if(to.path === '/goods/reports') next('/reports')
  if(! sessionStorage.getItem('token'))  next('/login')
  next()
})
export default router
